var positionContrllers = angular.module('positionContrllers', []);

positionContrllers.controller('PositionCtrl',['$scope','$http',
                                            
  function($scope,$http){

	$http.get('/mvc/position/listdata').success(function(data){
		$scope.PosList=data;
	});
	
	
	$scope.formData={};
	
	$scope.newDept = function(){
		$scope.edit_title = "New Position: ";
		$scope.isEditing = true;
		$scope.formData={};
	};
	
	$scope.edit = function(dept){
		$scope.edit_title = "Edit Position: ";
		$scope.isEditing = true;
		$scope.formData.id = dept.id;
		$scope.formData.name = dept.name;
		$scope.formData.nurseryId = dept.nurseryId;
	};
	
	$scope.add = function(dept){
		
		if(dept.name == ""){
			alert("Please input the position name.");
			return;
		}
		
		$http(
		{
			method:'POST',
			url:'/mvc/position/add',
			data:$.param(dept),
			headers : {'Content-Type':'application/x-www-form-urlencoded'}
		}).success(
			function(data, status, headers, config){
				$scope.PosList=data;
				$scope.isEditing = false;
			}
		);
	};
	
	$scope.update = function(dept){
		var flag = true;
		
		if(flag){
			if(dept.name == null || dept.name == ""){
				alert("Please input the position name.");
				flag = false;
				return;
			}
		}
		
		if(flag){
			$http(
			{
				method:'POST',
				url:'/mvc/position/update',
				data:$.param(dept),
				headers : {'Content-Type':'application/x-www-form-urlencoded'}
			}).success(
				function(data, status, headers, config){
					$scope.PosList=data;
					$scope.isEditing = false;
				}
			);
		}
	};
	
	
	$scope.remove = function(dept){
		if(confirm("Do you really want to delete ?")){
			
			$scope.formData.id = dept.id;
			$scope.formData.name = dept.name;
			$scope.formData.nurseryId = dept.nurseryId;
			
			$http({
				method:'POST',
				url:'/mvc/position/delete',
				data:$.param($scope.formData),
				headers : {'Content-Type':'application/x-www-form-urlencoded'}
			}).success(
				function(data, status, headers, config){
					$scope.PosList=data;
				}
			);
		}
	};
}]);